package com.guagua.system.security;

import com.guagua.common.exception.BusinessException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class SecurityUtils {

	public static Long getCurrentUserId() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		if (authentication == null || !authentication.isAuthenticated()) {
			throw new BusinessException("用户未登录");
		}

		Object principal = authentication.getPrincipal();
		if (principal instanceof BlogUserDetails) {
			return ((BlogUserDetails) principal).getId();
		}

		throw new BusinessException("获取用户信息失败");
	}

	public static BlogUserDetails getCurrentUser() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		if (authentication == null || !authentication.isAuthenticated()) {
			throw new BusinessException("用户未登录");
		}

		Object principal = authentication.getPrincipal();
		if (principal instanceof BlogUserDetails) {
			return (BlogUserDetails) principal;
		}

		throw new BusinessException("获取用户信息失败");
	}
} 